package model.service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import model.dao.MovieDao;
import model.vo.movie.MovieVO;

public class MovieService {
	private MovieDao movieDao;
	public void setMovieDao(MovieDao movieDao) {
		this.movieDao = movieDao;
	}
	/**
	 * 현재 상영작
	 */
	public ArrayList<HashMap> getScreeningMovie() throws SQLException{
		return movieDao.getScreeningMovie();		
	}
	/**
	 * 상영예정작
	 * @throws SQLException 
	 */
	public ArrayList<HashMap> getUpcomingMovie() throws SQLException{
		return movieDao.getUpcomingMovie();
	}

	public MovieVO getMovieDetail(int movie_no) throws SQLException {
		return movieDao.getMovieInfo(movie_no);
	}
	public ArrayList<HashMap> getBoxOffice() throws SQLException {
		ArrayList<HashMap> map = movieDao.getBoxOfficeList();		
		return map;
	}
	public ArrayList<HashMap> getMainMovieList() throws SQLException {
		return movieDao.getMainMovieList();
	}
	/**
	 * 5/9수정사항. 박스오피스 리스트
	 * @return
	 * @throws SQLException 
	 */
	public ArrayList<HashMap> getBoxOfficeList() throws SQLException {
		ArrayList<HashMap> list = new ArrayList<HashMap>();
		ArrayList<HashMap> movieList = movieDao.getBoxOfficeList();
		
		int total=0;
		for(HashMap map : movieList){
			total+=Integer.parseInt(((BigDecimal) map.get("TOTAL_MEMBER")).toString());	
		} //전체 관람객 수
		System.out.println("전체 관람객 수 : "+total);
		if(total==0){	//최초에 전체 관람객 수가 없을 경우 
			total=1;
		}
		for(HashMap map : movieList){
			//해당 영화의 누적 관객수
			int count = Integer.parseInt(((BigDecimal) map.get("TOTAL_MEMBER")).toString());
			//전체 영화의 누적 관객수로 나누고 *100
			float viewRate =Math.round(((float)(count)/(float)(total))*100);	//52/75
			System.out.println("누적관객수 : "+count+",관람률 : "+viewRate);
			map.put("RATE",viewRate);
			list.add(map);
		}
		return list;
	}
	public String dayCheck(int no) throws SQLException {
		return movieDao.dayCheck(no);
	}
	}
